# -*- coding: UTF-8 -*-
# Author: Damon(wuud1)
# CreateDate: 
# Message:  根据保养表获取保养分组表

from untils.MysqlTools import MysqlTools


class GetMaintenanceStructureCollection:
    def __init__(self, **kwargs):
        self.db = MysqlTools(db_config=kwargs.get("db"), type='dict', auto_commit=1000)
        self.part_num_table = kwargs.get("part_num_table")
        self.structure_collection_table = kwargs.get("structure_collection_table")
        self.num = 0
        self.mtype_name = {
            "1": "发动机油",
            "2": "机油滤清器滤芯",
            "3": "空气滤清器滤芯",
            "4": "空调滤清器滤芯",
            "5": "燃油滤清器",
            "6": "变速箱滤芯",
            "7": "火花塞",
            "8": "前制动盘",
            "9": "后制动盘",
            "10": "前制动片",
            "11": "后制动片",
            "12": "制动感应线",
            "13": "前挡雨刮片",
            "14": "后窗雨刮片",
            "15": "蓄电池",
            "16": "多楔皮带",
            "17": "变速箱油",
            "18": "制动液",
            "19": "防冻液",
        }
        self.mtype_name_old = {
            "1": "机油机滤",
            "2": "空气滤清器",
            "3": "",
            "4": "微尘滤清器",
            "5": "燃油滤清器",
            "6": "制动液",
            "7": "火花塞",
            "8": "后制动片",
            "9": "前制动片",
            "10": "变速箱油、变速箱滤芯",
            "11": "前部制动盘",
            "12": "后部制动盘",
            "13": "后雨刮片",
            "14": "防冻液",
            "15": "蓄电池",
            "16": "前雨刮片",
            "17": "",
            "18": "多楔皮带",
            "19": "",
        }
        self.mtype_sequence = {
            "1":"1",
            "2":"2",
            "3":"3",
            "4":"4",
            "5":"5",
            "6":"6",
            "7":"7",
            "8":"8",
            "9":"9",
            "10":"10",
            "11":"11",
            "12":"12",
            "13":"13",
            "14":"14",
            "15":"15",
            "16":"16",
            "17":"17",
            "18":"18",
            "19":"19",
        }
        self.mtype_sequence_old = {
            "1": "1",
            "2": "2",
            "3": "",
            "4": "3",
            "5": "4",
            "6": "13",
            "7": "5",
            "8": "10",
            "9": "9",
            "10": "6",
            "11": "11",
            "12": "12",
            "13": "8",
            "14": "14",
            "15": "15",
            "16": "7",
            "17": "",
            "18": "16",
            "19": "",
        }
        self.freq = {
            "1": "10000km",
            "2": "20000km",
            "3": "",
            "4": "20000km",
            "5": "20000km",
            "6": "20000km",
            "7": "40000km",
            "8": "50000km",
            "9": "50000km",
            "10": "60000km",
            "11": "100000km",
            "12": "100000km",
            "13": "20000km",
            "14": "260000km",
            "15": "",
            "16": "20000km",
            "17": "10000km",
            "18": "",
            "19": "",
        }

    def get_part_data(self):
        sql = '''select distinct `brandCode`,`cid`,`m_type` from `{}`'''.format(self.part_num_table)
        data = self.db.select_all(sql)
        return data

    def run(self, repair=False):

        data_list = self.get_part_data()
        del_sql = '''truncate table {}'''.format(self.structure_collection_table)
        self.db.execute_sql(del_sql, commit=True)
        print("开始循环...")
        for i in data_list:
            brandCode = i.get("brandCode")
            cid = i.get("cid")
            m_type = i.get("m_type")
            freq = self.freq.get(m_type)
            if m_type == '17':
                print(brandCode)
            name = self.mtype_name[m_type]
            sequence = int(self.mtype_sequence[m_type])
            if repair:
                name = self.mtype_name_old[m_type]
                sequence = int(self.mtype_sequence_old[m_type])
            insert_sql = '''insert into {} (`cid`,`m_type`,`freq`,`sequence`,`brandCode`,`name`,`extra_info`,`updatetime`,`createtime`) VALUES ("%s","%s","%s",%d,"%s","%s","",now(),now())'''.format(
                self.structure_collection_table) % (cid, m_type, freq, sequence, brandCode, name)
            self.db.execute_sql(insert_sql)
        self.db.commit_sql()


if __name__ == '__main__':

    db_3306 = [
        # "audi",
        "benz",
        # "bmw",
        # "citroen",
        # "hyundai",
        # "jaguar",
        # "kia",
        # "land_rover",
        # "mclaren",
        # "peugeot",
        # "porsches",
        # "skoda",
        # "volvos",
        # "vwag",
    ]
    db_3307 = [
        "baic",
        "bentley",
        "bullstuff",
        "ferrari",
        "honda",
        "maserati",
        "saicgm",
        "toyota",
    ]
    for brand in db_3306:
        DB = brand
        if brand == "benz":
            DB = "benzs"
        db = dict(
            host="192.168.10.166",
            port=3306,
            user="a",
            passwd="110",
            db=DB,
            charset="utf8",
        )
        part_num_table = 'maintenance_part_num_{}_repair'.format(brand)
        structure_collection_table = 'maintenance_structure_collection_{}_repair'.format(brand)
        tools = GetMaintenanceStructureCollection(db=db,part_num_table=part_num_table,structure_collection_table=structure_collection_table)
        tools.run(repair=True)  # 如果是repair表的时候调整为True, 要记得改上边表名!!!



